package com.harboursoftware.xstorage.unit.ac

import com.harboursoftware.xstorage.ac.AccessControlPolicy 
import com.harboursoftware.xstorage.ac.CanonicalUserGrantee 
import com.harboursoftware.xstorage.ac.Grant 
import com.harboursoftware.xstorage.ac.Permission 
import com.harboursoftware.xstorage.model.XUser 
import org.junit.Assert 
import org.junit.Before 
import org.junit.After
import org.junit.Test 

/**
 * Test case from <a href="http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectGETacl.html">GET Object acl</a>.
 */
class AccessControlPolicyTest {
    def xml = '<AccessControlPolicy><Owner><ID>8a6925ce4adf588a4532aa379004fef</ID><DisplayName>mtd@amazon.com</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>8a6925ce4adf588a453214a379004fef</ID><DisplayName>mtd@amazon.com</DisplayName></Grantee><Permission>FULL_CONTROL</Permission></Grant></AccessControlList></AccessControlPolicy>' 
    def acp 
    @Before
    void setUp() {
        def owner = new XUser()
        owner.id = '8a6925ce4adf588a4532aa379004fef'
        owner.displayName = 'mtd@amazon.com'
        def grantee = new CanonicalUserGrantee('8a6925ce4adf588a453214a379004fef', 'mtd@amazon.com')
        def grant = new Grant(grantee, Permission.FULL_CONTROL) 
        acp = new AccessControlPolicy(owner, [grant])
    }
    
    @After
    void tearDown() {
        acp = null
    }
    
    @Test
	void test_ToXML() {
        Assert.assertEquals(xml, acp.toXML())
    }
    
    
    @Test
    void test_fromXML() {
        def generatedAcp = AccessControlPolicy.fromXML(xml)
        println acp
        println generatedAcp
        assert acp == generatedAcp
    }
    
    @Test
    void test_equals_false() {
        def user = new XUser(id : 'testuser')
        def acp1 = AccessControlPolicy.createCannedAccessPolicy(AccessControlPolicy.CANNED_ACP_PRIVATE, user, user)
        def acp2 = AccessControlPolicy.createCannedAccessPolicy(AccessControlPolicy.CANNED_ACP_PUBLIC_READE, user, user)

        assert acp1.equals(acp2) == false
    }
}